var express = require("express");
var router = express.Router();
var db = require("../model/index");
/* 查询收货人信息 */
router.get("/orders/getallor", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='待付款'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
/* 查询所有商品 */
router.get("/orders/getallorders", function (req, res, next) {
    let mysql = ` SELECT t1.id,t1.ordernum,t1.subtime,t1.userid,t1.paymethod,t1.handletime,t1.source,t2.phone,t1.money,t1.type
    FROM orders AS t1 LEFT JOIN USER AS t2 ON t1.userid=t2.id`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
/* 删除退货信息列表 */
router.post("/orders/delorder", function (req, res, next) {
    let mysql = `DELETE FROM orders WHERE id = ${req.body.id};`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "删除退货信息失败，请稍后重试",
                data: err,
            });
        }
        res.json({
            code: 200,
            msg: "删除退货信息成功",
            data: data,
        });
    });
});
// 修改退货信息
router.post("/ModifyData", function (req, res, next) {
    console.log(req.body);
    let mysql = `UPDATE  orders SET ordernum = ?,subtime= ?,money = ?, type=? WHERE id = ?`;
    console.log("req", req.body);
    db(
        mysql,
        [
            req.body.ordernum,
            req.body.subtime,
            req.body.money,
            req.body.type,
            req.body.id,
        ],
        function (err, data) {
            console.log("err", err);
            if (err) {
                return res.json({
                    code: 500,
                    msg: "修改退货信息失败，请稍后重试",
                });
            }
            res.json({
                code: 200,
                msg: "修改退货信息成功",
                data: data,
            });
        }
    );
});
/* 批量删除退款信息 */
router.post("/refund_information", function (req, res, next) {
    let mysql = `DELETE FROM orders WHERE `;
    for (let i = 0; i < req.body.length; i++) {
        mysql = mysql + `id=${req.body[i].id}` + " or ";
    }
    mysql = mysql.replace(/or $/g, "");
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "批量删除退款信息失败，请稍后重试",
                data: err,
            });
        }
        res.json({
            code: 200,
            msg: "批量删除退款信息成功",
            data: data,
        });
    });
});
// 退款信息的模糊查询
router.get("/orders/getChaXun", function (req, res, next) {
    let mysql = `
    SELECT t1.id,t1.ordernum,t1.subtime,t1.userid,t1.paymethod,t1.source,t2.phone,t1.money,t1.type
    FROM orders AS t1 LEFT JOIN USER AS t2 ON t1.userid=t2.id
    WHERE phone LIKE '%${req.query.phone}%'   AND ordernum LIKE '%${req.query.search}%'
    AND SUBTIME LIKE '%${req.query.time}%'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getChaXun1", function (req, res, next) {
    let mysql = `SELECT * FROM orders 
    WHERE ordernum LIKE '%${req.query.ordernum}%'   AND userid LIKE '%${req.query.userid}%'
    AND SUBTIME LIKE '%${req.query.time}%'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
// 查询所有退换货列表
router.get("/orders/getReturnOrders", function (req, res, next) {
    let mysql = `SELECT a.id,a.reasontype,a.subtime,a.handletime,d.phone,c.describe,b.type,b.money,b.ordernum FROM order_change a LEFT JOIN  orders b ON a.orderid =b.id
    LEFT JOIN  order_reason c ON a.orderid =c.id
    LEFT JOIN  USER d ON b.userid =d.id `;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
// 退换货模糊筛选
router.get("/orders/getsearch", function (req, res, next) {
    let mysql = ` SELECT a.id,a.reasontype,a.subtime,a.handletime,d.phone,c.describe,b.type,b.money,b.ordernum FROM order_change a LEFT JOIN  orders b ON a.orderid =b.id
    LEFT JOIN  order_reason c ON a.orderid =c.id
    LEFT JOIN  USER d ON b.userid =d.id
       WHERE phone LIKE '%${req.query.phone}%'   AND ordernum LIKE '%${req.query.search}%'
       AND TYPE LIKE '%${req.query.type}%'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
/* 批量删除退换货信息 */
router.post("/Delinformation", function (req, res, next) {
    let mysql = ` SELECT a.id,a.reasontype,a.subtime,a.handletime,d.phone,c.describe,b.type,b.money,b.ordernum FROM order_change a LEFT JOIN  orders b ON a.orderid =b.id
    LEFT JOIN  order_reason c ON a.orderid =c.id
    LEFT JOIN  USER d ON b.userid =d.id `;
    for (let i = 0; i < req.body.length; i++) {
        mysql = mysql + `id=${req.body[i].id}` + " or ";
    }
    mysql = mysql.replace(/or $/g, "");
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "批量删除退款信息失败，请稍后重试",
                data: err,
            });
        }
        res.json({
            code: 200,
            msg: "批量删除退款信息成功",
            data: data,
        });
    });
});
// 查看详情订单查询
router.get("/ordersreturn/getreturns", function (req, res, next) {
    let mysql = `SELECT a.id,a.reasontype,a.subtime,a.handletime,d.phone,c.describe,b.type,b.money,b.ordernum FROM order_change a LEFT JOIN  orders b ON a.orderid =b.id
    LEFT JOIN  order_reason c ON a.orderid =c.id
    LEFT JOIN  USER d ON b.userid =d.id `;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
// 查看详情金额查询
router.get("/ordersreturn/orderAmount", function (req, res, next) {
    let mysql = `SELECT b.money,a.reasontype,c.describe FROM order_change a LEFT JOIN  orders b ON a.orderid =b.id
    LEFT JOIN  order_reason c ON a.orderid =c.id
    LEFT JOIN  USER d ON b.userid =d.id  `;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});


module.exports = router;
